package com.kakavr.user.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.order.service.IPurchaseHistoryService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.SessionUtils;
/**
 * 玩家消费记录
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("userOrder")
public class UserOrderController extends BaseController {
	Logger logger=Logger.getLogger(UserOrderController.class);
	
	@Resource
	private IPurchaseHistoryService purchaseHistoryService;

	/**
	 * 消费记录
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectOrderList(HttpServletRequest request) {
		Map<String, Object> map = this.initPaginationParams(request);
		map.put("userId", SessionUtils.getUserId());
		logger.info("++++++用户查询消费记录"+JSON.toJSONString(SessionUtils.getCurUser()));
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseListByPage(map));
	}

	/**
	 * 消费详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectInfo", method = RequestMethod.GET)
	public Map<String, Object> selectOrderInfo(HttpServletRequest request) {
		String purchaseId = request.getParameter("purchaseId");
		if (StringUtils.isBlank(purchaseId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("purchaseId", purchaseId);
		return MsgResult.okResultData(purchaseHistoryService
				.selectPurchaseInfo(map));
	}
	
	
	/**
	 * 游戏评价
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "evaluate", method = RequestMethod.POST)
	public Map<String, Object> evaluate(HttpServletRequest request){
		String purchaseId = request.getParameter("purchaseId");
		if (StringUtils.isBlank(purchaseId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "消费id");
		}
		String evaluate = request.getParameter("evaluate");
		if(StringUtils.isBlank(evaluate)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "评价");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("purchaseId", purchaseId);
		map.put("evaTime", DateTime.now().toDate());
		map.put("isEva", 1);
		map.put("evaluate", evaluate);
		purchaseHistoryService.updatePurchaseHistoryByEva(map);
		return MsgResult.okResult();
	}

}
